package com.smcdd.cdb.dbo.cdb_dbo.network.error_exception;


import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.io.IOException;
import java.net.ConnectException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * 作者：Administrator on 2017/9/8 10:24
 * 邮箱：zhanghuaiha@gmail.com
 */

public class ApiErrorHelper {
    public static void handleCommonError(Throwable e){
        if (e instanceof ConnectException) {
            ToastUtil.showToast("没有网络,请检查你的网络");
        }else if (e instanceof HttpException) {
            ToastUtil.showToast("网络异常,请重试......");
        }else if (e instanceof IOException) {
            ToastUtil.showToast("数据加载错误,请重试");
        }else if (e instanceof ApiException) {
            ToastUtil.showToast(e.getMessage());
        }else {
            judge(e.getMessage());
        }
    }

    private static String judge(String msg)  {

        if (msg.indexOf("50") != -1) {
            return "网络异常，请重试";
        }else if (msg.indexOf("40") != -1) {
            return "网络请求失败，请重试";
        }else if (msg.indexOf("41") != -1) {
            return "网络请求失败，请重试";
        }else if (msg.indexOf("30") != -1) {
            return "网络加载失败，请重试";
        }else{
            return msg;
        }
    }
}
